<template>
  <div>
    <div class="filter-container mt-15">
      <el-button
        v-if="checkPermission(permissionRoles.behavior_config_download)"
        v-waves
        :loading="downloadLoading"
        type="primary"
        icon="el-icon-download"
        class="filter-item"
        @click="handleDownload()"
      >导出</el-button>
      <el-upload
        v-if="checkPermission(permissionRoles.behavior_config_upload)"
        ref="upload"
        v-waves
        class="filter-item"
        action="api/gm/config/behavior/upload"
        :headers="header"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        :before-upload="beforeUpload"
        :on-success="successInfo"
        :on-error="errorInfo"
        :limit="1"
        :on-exceed="handleExceed"
        :on-change="handleChange"
        :file-list="fileList"
        :auto-upload="true"
        :accept="'.csv,.xlsx,.xls,.txt'"
      >
        <el-button type="success" icon="el-icon-upload2">上传</el-button>
      </el-upload>
      <el-button type="primary" class="filter-item" @click="type_desc">查看类型</el-button>
    </div>
    <el-table ref="table" v-loading="listLoading" :data="list" border stripe height="680">

      <el-table-column
        v-for="item in data_list"
        :key="item.name"
        :label="item.label"
        :prop="item.name"
        :width="item.width || 105"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row[item.name] }}
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
const data_x = 10
const data_list = [
  { label: '日志分类ID', name: 'cid', width: 80 },
  { label: '日志分类名称', name: 'cname', width: 80 },
  { label: '日志ID', name: 'id', width: 50 },
  { label: '日志名称', name: 'name', width: 105 }
]
for (let i = 1; i <= data_x; i++) {
  data_list.push({ label: '数据' + i + '名称', name: 'data' + i })
  data_list.push({ label: '数据' + i + '显示类型', name: 'type' + i })
}
import waves from '@/directive/waves' // 实现点击波浪纹
import Pagination from '@/components/Pagination'
import checkPermission from '@/utils/permission'
import { getBehaviorConfig } from '@/api/actorInfo'

export default {
  name: 'BehaviorLog',
  directives: { waves },
  components: { Pagination },
  data() {
    return {
      permissionRoles: this.$store.state.user.permissionRoles,
      header: {
        Authorization: this.$store.state.user.token
      },
      total: 0,
      list: [],
      data_list: data_list,
      listLoading: false,
      downloadLoading: false,
      listQuery: {
        page: 1,
        limit: 200
      },
      fileList: []
    }
  },
  computed: {
    tHeader() {
      return this.data_list.map(item => item.label)
    },
    filterVal() {
      return this.data_list.map(item => item.name)
    }
  },
  created() {
    this.init()
  },
  methods: {
    type_desc() {
      this.$alert(`A:显示原始记录<br>
B:关联道具ID，显示道具名称<br>
C:类型枚举，格式："C,1:真实充值;2:GM指令;3:内币充值"<br>
D:关联充值ID，显示充值挡位名称<br>
E:时间戳转化为日期时间显示<br>
F:关联采用配置表里的配置类型，格式F1,F2<br>
G:大额数字显示为小额数字加大额单位：比如万，亿，兆等<br>
H:多道具显示，如：道具名(ID)*100,道具名(ID)*200<br>
`, '类型说明', {
        showConfirmButton: false,
        dangerouslyUseHTMLString: true, // 允许解析 HTML 字符串
        callback: action => {
          // this.$message({
          //   type: 'info',
          //   message: `action: ${action}`
          // })
        }
      })
      return
    },
    checkPermission,
    init() {
      this.getList()
    },
    async getList() {
      this.listLoading = true
      const res = await getBehaviorConfig()
      this.listLoading = false
      this.list = res.data.items
      this.total = res.data.total
      this.$nextTick(() => {
        this.$refs.table.doLayout()
      })
    },

    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePreview(file) {
      console.log(file)
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    beforeUpload(file) {
      console.log(file)
      return true
    },
    handleAvatarSuccess(response, file, fileList) {
      this.$refs.fileUploadBtn.clearFiles()
    },
    handleChange(files, fileList) {
      console.log(files, fileList)
    },
    errorInfo(err, file, fileList) {
      this.$message.error(`上传失败：${err}`)
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = this.tHeader
        const filterVal = this.filterVal
        const data = this.formatJson(filterVal)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: '日志配置'
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        return v[j]
      }))
    },
    successInfo(res, file, fileList) {
      this.$refs.upload.clearFiles()
      this.init()
      if (res.code === 40003) {
        this.$message.error(res.message)
      } else {
        this.$refs.upload.clearFiles()
        this.visible = false
        this.$message.success(res.message)
      }
    }
  }
}
</script>
